<template>
  <div class="app-container">
    <el-form :model="queryParams_PeiJian" ref="queryForm_peijian" size="small" :inline="true" v-show="showSearch_peijian" label-width="68px">
      <el-form-item label="配件编号" prop="accessoryId">
        <el-input
          v-model="queryParams_PeiJian.accessoryId"
          placeholder="请输入配件编号"
          clearable
          @keyup.enter.native="handleQuery_peijian"
        />
      </el-form-item>
      <el-form-item label="配件名称" prop="accessoryName">
        <el-input
          v-model="queryParams_PeiJian.accessoryName"
          placeholder="请输入配件名称"
          clearable
          @keyup.enter.native="handleQuery_peijian"
        />
      </el-form-item>
      <el-form-item label="入库量" prop="stockInput">
        <el-input
          v-model="queryParams_PeiJian.stockInput"
          placeholder="请输入入库量"
          clearable
          @keyup.enter.native="handleQuery_peijian"
        />
      </el-form-item>
      <el-form-item label="出库量" prop="stockOutput">
        <el-input
          v-model="queryParams_PeiJian.stockOutput"
          placeholder="请输入出库量"
          clearable
          @keyup.enter.native="handleQuery_peijian"
        />
      </el-form-item>
      <el-form-item label="库存量" prop="stockQuantity">
        <el-input
          v-model="queryParams_PeiJian.stockQuantity"
          placeholder="请输入库存量"
          clearable
          @keyup.enter.native="handleQuery_peijian"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery_peijian">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery_peijian">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd_peijian"
          v-hasPermi="['system:accessory_info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single_peijian"
          @click="handleUpdate_peijian"
          v-hasPermi="['system:accessory_info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple_peijian"
          @click="handleDelete_peijian"
          v-hasPermi="['system:accessory_info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport_peijian"
          v-hasPermi="['system:accessory_info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch_peijian" @queryTable="getList_peijian"></right-toolbar>
    </el-row>

    <el-table v-loading="loading_peijian" :data="accessory_infoList" @selection-change="handleSelectionChange_peijian">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配件编号" align="center" prop="accessoryId" />
      <el-table-column label="配件名称" align="center" prop="accessoryName" />
      <el-table-column label="入库量" align="center" prop="stockInput" />
      <el-table-column label="出库量" align="center" prop="stockOutput" />
      <el-table-column label="库存量" align="center" prop="stockQuantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate_peijian(scope.row)"
            v-hasPermi="['system:accessory_info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete_peijian(scope.row)"
            v-hasPermi="['system:accessory_info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total_peijian>0"
      :total="total_peijian"
      :page.sync="queryParams_PeiJian.pageNum"
      :limit.sync="queryParams_PeiJian.pageSize"
      @pagination="getList_peijian"
    />

    <!-- 添加或修改配件信息对话框 -->
    <el-dialog :title="title_peijian" :visible.sync="open_peijian" width="500px" append-to-body>
      <el-form ref="form_peijian" :model="form_peijian" :rules="rules_peijian" label-width="80px">
        <el-form-item label="配件编号" prop="accessoryId">
          <el-input v-model="form_peijian.accessoryId" placeholder="请输入配件编号" />
        </el-form-item>
        <el-form-item label="配件名称" prop="accessoryName">
          <el-input v-model="form_peijian.accessoryName" placeholder="请输入配件名称" />
        </el-form-item>
        <el-form-item label="入库量" prop="stockInput">
          <el-input v-model="form_peijian.stockInput" placeholder="请输入入库量" />
        </el-form-item>
        <el-form-item label="出库量" prop="stockOutput">
          <el-input v-model="form_peijian.stockOutput" placeholder="请输入出库量" />
        </el-form-item>
        <el-form-item label="库存量" prop="stockQuantity">
          <el-input v-model="form_peijian.stockQuantity" placeholder="请输入库存量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm_peijian">确 定</el-button>
        <el-button @click="cancel_peijian">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccessory_info, getAccessory_info, delAccessory_info, addAccessory_info, updateAccessory_info } from "@/api/4s_shop/accessory_info";

export default {
  name: "Accessory_info",
  data() {
    return {
      // 遮罩层
      loading_peijian: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single_peijian: true,
      // 非多个禁用
      multiple_peijian: true,
      // 显示搜索条件
      showSearch_peijian: true,
      // 总条数
      total_peijian: 0,
      // 配件信息表格数据
      accessory_infoList: [],
      // 弹出层标题
      title_peijian: "",
      // 是否显示弹出层
      open_peijian: false,
      // 查询参数
      queryParams_PeiJian: {
        pageNum: 1,
        pageSize: 10,
        accessoryId: null,
        accessoryName: null,
        stockInput: null,
        stockOutput: null,
        stockQuantity: null
      },
      // 表单参数
      form_peijian: {},
      // 表单校验
      rules_peijian: {
        accessoryId: [
          { required: true, message: "配件编号不能为空", trigger: "blur" }
        ],
        accessoryName: [
          { required: true, message: "配件名称不能为空", trigger: "blur" }
        ],
        stockInput: [
          { required: true, message: "入库量不能为空", trigger: "blur" }
        ],
        stockOutput: [
          { required: true, message: "出库量不能为空", trigger: "blur" }
        ],
        stockQuantity: [
          { required: true, message: "库存量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList_peijian();
  },
  methods: {
    /** 查询配件信息列表 */
    getList_peijian() {
      this.loading_peijian = true;
      listAccessory_info(this.queryParams_PeiJian).then(response => {
        this.accessory_infoList = response.rows;
        this.total_peijian = response.total_peijian;
        this.loading_peijian = false;
      });
    },
    // 取消按钮
    cancel_peijian() {
      this.open_peijian = false;
      this.reset_peijian();
    },
    // 表单重置
    reset_peijian() {
      this.form_peijian = {
        accessoryId: null,
        accessoryName: null,
        stockInput: null,
        stockOutput: null,
        stockQuantity: null
      };
      this.resetForm("form_peijian");
    },
    /** 搜索按钮操作 */
    handleQuery_peijian() {
      this.queryParams_PeiJian.pageNum = 1;
      this.getList_peijian();
    },
    /** 重置按钮操作 */
    resetQuery_peijian() {
      this.resetForm("queryForm_peijian");
      this.handleQuery_peijian();
    },
    // 多选框选中数据
    handleSelectionChange_peijian(selection) {
      this.ids = selection.map(item => item.accessoryId)
      this.single_peijian = selection.length!==1
      this.multiple_peijian = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd_peijian() {
      this.reset_peijian();
      this.open_peijian = true;
      this.title_peijian = "添加配件信息";
    },
    /** 修改按钮操作 */
    handleUpdate_peijian(row) {
      this.reset_peijian();
      const accessoryId = row.accessoryId || this.ids
      getAccessory_info(accessoryId).then(response => {
        this.form_peijian = response.data;
        this.open_peijian = true;
        this.title_peijian = "修改配件信息";
      });
    },
    /** 提交按钮 */
    submitForm_peijian() {
      this.$refs["form_peijian"].validate(valid => {
        if (valid) {
          if (this.form_peijian.accessoryId != null) {
            updateAccessory_info(this.form_peijian).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open_peijian = false;
              this.getList_peijian();
            });
          } else {
            addAccessory_info(this.form_peijian).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open_peijian = false;
              this.getList_peijian();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete_peijian(row) {
      const accessoryIds = row.accessoryId || this.ids;
      this.$modal.confirm('是否确认删除配件信息编号为"' + accessoryIds + '"的数据项？').then(function() {
        return delAccessory_info(accessoryIds);
      }).then(() => {
        this.getList_peijian();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport_peijian() {
      this.download('system/accessory_info/export', {
        ...this.queryParams_PeiJian
      }, `accessory_info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
